﻿#region 程序集引用
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Text.RegularExpressions;
#endregion

namespace Zhang
{
    /// <summary>
    /// Object扩展类
    /// </summary>
    public static class Expands
    {
        /// <summary>
        /// 分页sql
        /// </summary>
        /// <param name="sql">查询语句</param>
        /// <param name="orderBy">排序</param>
        /// <param name="pageIndex">第几页</param>
        /// <param name="pageSize">一页多少条</param>
        /// <returns></returns>
        public static string ToPageSql(this string sql, string orderBy, int pageIndex, int pageSize)
        {
            pageIndex = pageIndex <= 0 ? 1 : pageIndex;
            var newSql = "select * from (select ROW_NUMBER() OVER (order by " + orderBy + ") AS Row,* from (" + sql + ") as TT) as TTT  " +
                "WHERE TTT.Row between " + ((pageIndex - 1) * pageSize + 1) + " and " + pageSize * pageIndex + " ;";
            return newSql;
        }
        /// <summary>
        /// sql 统计数量
        /// </summary>
        /// <param name="sql">原生sql语句</param>
        /// <returns></returns>
        public static string ToCountSql(this string sql)
        {
            return " select count(1) from (" + sql + ") as TT ;";
        }

        /// <summary>
        /// null转DbNull
        /// </summary>
        /// <param name="val"></param>
        /// <returns></returns>
        public static object IsDdNull(this object val)
        {
            return val ?? DBNull.Value;
        }

        /// <summary>
        /// null转DbNull
        /// </summary>
        /// <param name="val"></param>
        /// <returns></returns>
        public static object IsMinDate(this DateTime val)
        {
            if (val.IsMinDateBool())
            {
                return DBNull.Value;
            }
            return val;
        }

        /// <summary>
        /// null转DbNull
        /// </summary>
        /// <param name="val"></param>
        /// <returns></returns>
        public static bool IsMinDateBool(this DateTime val)
        {
            return val == DateTime.MinValue;
        }

        /// <summary>
        /// 转化INT
        /// </summary>
        /// <param name="val"></param>
        /// <param name="del">转化失败默认值</param>
        /// <returns></returns>
        public static int ToInt(this object val, int del = 0)
        {
            try
            {
                if (val != null && val != DBNull.Value)
                {
                    del = Convert.ToInt32(val);
                }
            }
            catch
            {
                del = 0;
            }
            return del;
        }
        /// <summary>
        /// 转decimal
        /// </summary>
        /// <param name="val"></param>
        /// <param name="del">转化失败默认值</param>
        /// <param name="num">保留几位小数</param>
        /// <returns></returns>
        public static decimal ToDecimal(this object val, decimal del = 0, int num = 2)
        {
            try
            {
                if (val != null)
                {
                    del = Convert.ToDecimal(val);
                }
            }
            catch
            {
                del = 0;
            }
            return Math.Round(del, num);
        }
        /// <summary>
        /// 转string
        /// </summary>
        /// <param name="val"></param>
        /// <param name="str">转化失败默认值</param>
        /// <returns></returns>
        public static string ToStr(this object val, string str = "")
        {
            if (val != null)
            {
                str = val.ToString();
            }
            return str;
        }
        /// <summary>
        /// 转bool
        /// </summary>
        /// <param name="val"></param>
        /// <param name="de">转化失败默认值</param>
        /// <returns></returns>
        public static bool ToBool(this object val, bool de = false)
        {
            try
            {
                if (val != DBNull.Value && val != null)
                {
                    de = Convert.ToBoolean(val);
                }
            }
            catch
            {
                de = false;
            }

            return de;
        }
        /// <summary>
        /// 转dateTime 失败时间为1900-01-01 00:00:00
        /// </summary>
        /// <param name="val"></param>
        /// <returns></returns>
        public static DateTime ToDateTime(this object val)
        {
            DateTime dt = DateTime.Parse("1900-01-01 00:00:00");
            try
            {
                if (val != DBNull.Value && val != null)
                {
                    dt = Convert.ToDateTime(val);
                }
            }
            catch
            {
                dt = DateTime.Parse("1900-01-01 00:00:00");
            }
            return dt;
        }
    }
}

